// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network.packets;

import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map.Entry;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoPlayer;
import zombie.characters.CharacterTimedActions.BaseAction;
import zombie.characters.CharacterTimedActions.LuaTimedActionNew;
import zombie.core.Core;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.debug.DebugLog;
import zombie.debug.DebugType;
import zombie.network.GameClient;
import zombie.network.GameServer;

public class ActionPacket implements INetworkPacket {
    private short id;
    private boolean operation;
    private float reloadSpeed;
    private boolean override;
    private String primary;
    private String secondary;
    private String action;
    private final HashMap<String, String> variables = new HashMap();
    private IsoGameCharacter character;

    public void set(boolean _operation, BaseAction _action) {
        this.character = _action.chr;
        this.id = _action.chr.getOnlineID();
        this.operation = _operation;
        this.reloadSpeed = _action.chr.getVariableFloat("ReloadSpeed", 1.0F);
        this.override = _action.overrideHandModels;
        this.primary = _action.getPrimaryHandItem() == null ? _action.getPrimaryHandMdl() : _action.getPrimaryHandItem().getStaticModel();
        this.secondary = _action.getSecondaryHandItem() == null ? _action.getSecondaryHandMdl() : _action.getSecondaryHandItem().getStaticModel();

        for (String string : _action.animVariables) {
            this.variables.put(string, _action.chr.getVariableString(string));
        }

        if (this.variables.containsValue("DetachItem") || this.variables.containsValue("AttachItem")) {
            this.variables.put("AttachAnim", _action.chr.getVariableString("AttachAnim"));
        }

        if (this.variables.containsValue("Loot")) {
            this.variables.put("LootPosition", _action.chr.getVariableString("LootPosition"));
        }

        if (_action instanceof LuaTimedActionNew) {
            this.action = ((LuaTimedActionNew)_action).getMetaType();
        }
    }

    @Override
    public void parse(ByteBuffer b, UdpConnection connection) {
        this.id = b.getShort();
        this.operation = b.get() != 0;
        this.reloadSpeed = b.getFloat();
        this.override = b.get() != 0;
        this.primary = zombie.GameWindow.ReadString(b);
        this.secondary = zombie.GameWindow.ReadString(b);
        this.action = zombie.GameWindow.ReadString(b);
        int int0 = b.getInt();

        for (int int1 = 0; int1 < int0; int1++) {
            this.variables.put(zombie.GameWindow.ReadString(b), zombie.GameWindow.ReadString(b));
        }

        if (GameServer.bServer) {
            this.character = (IsoGameCharacter)GameServer.IDToPlayerMap.get(this.id);
        } else if (GameClient.bClient) {
            this.character = (IsoGameCharacter)GameClient.IDToPlayerMap.get(this.id);
        } else {
            this.character = null;
        }
    }

    @Override
    public void write(ByteBufferWriter b) {
        b.putShort(this.id);
        b.putBoolean(this.operation);
        b.putFloat(this.reloadSpeed);
        b.putBoolean(this.override);
        b.putUTF(this.primary);
        b.putUTF(this.secondary);
        b.putUTF(this.action);
        b.putInt(this.variables.size());

        for (Entry entry : this.variables.entrySet()) {
            b.putUTF((String)entry.getKey());
            b.putUTF((String)entry.getValue());
        }
    }

    @Override
    public boolean isConsistent() {
        boolean _boolean = this.character instanceof IsoPlayer;
        if (!_boolean && Core.bDebug) {
            DebugLog.log(DebugType.Multiplayer, "[Action] is not consistent");
        }

        return _boolean;
    }

    @Override
    public String getDescription() {
        StringBuilder stringBuilder = new StringBuilder("[ ").append("character=").append(this.id);
        if (this.isConsistent()) {
            stringBuilder.append(" \"").append(((IsoPlayer)this.character).getUsername()).append("\"");
        }

        stringBuilder.append(" | ")
            .append("action=")
            .append(this.action)
            .append(" | ")
            .append("operation=")
            .append(this.operation ? "start" : "stop")
            .append(" | ")
            .append("variables=")
            .append(this.variables.size())
            .append(" | ");

        for (Entry entry : this.variables.entrySet()) {
            stringBuilder.append((String)entry.getKey()).append("=").append((String)entry.getValue()).append(" | ");
        }

        stringBuilder.append("override=")
            .append(this.override)
            .append(" ")
            .append("primary=\"")
            .append(this.primary == null ? "" : this.primary)
            .append("\" ")
            .append("secondary=\"")
            .append(this.secondary == null ? "" : this.secondary)
            .append("\" ]");
        return stringBuilder.toString();
    }

    public boolean isRelevant(UdpConnection connection) {
        return this.isConsistent() && connection.RelevantTo(this.character.getX(), this.character.getY());
    }

    public void process() {
        if (this.isConsistent()) {
            if (this.operation) {
                BaseAction baseAction = new BaseAction(this.character);
                this.variables.forEach((string1, string0) -> {
                    if (!"true".equals(string0) && !"false".equals(string0)) {
                        baseAction.setAnimVariable(string1, string0);
                    } else {
                        baseAction.setAnimVariable(string1, Boolean.parseBoolean(string0));
                    }
                });
                if ("Reload".equals(this.variables.get("PerformingAction"))) {
                    this.character.setVariable("ReloadSpeed", this.reloadSpeed);
                }

                this.character.setVariable("IsPerformingAnAction", true);
                this.character.getNetworkCharacterAI().setAction(baseAction);
                this.character.getNetworkCharacterAI().setOverride(this.override, this.primary, this.secondary);
                this.character.getNetworkCharacterAI().startAction();
            } else if (this.character.getNetworkCharacterAI().getAction() != null) {
                this.character.getNetworkCharacterAI().stopAction();
            }
        } else {
            DebugLog.Multiplayer.warn("Action error: player id=" + this.id + " not fond");
        }
    }
}
